class Observer {
    constructor(data) {
        this.data = data
        this.walk(data)
    }

    // add getter & setter to data
    walk(data) {
        if (!data || typeof data !== 'object') {
            return
        }
        Object.keys(data).forEach(key => {
            this.defReactive(data, key, data[key])
            this.walk(data[key])
        })

    }


    defReactive(obj, key, val) {
        let _this=this;

        Object.defineProperty(data, key, {
            enumerable: true,
            configurable: true,
            get() {
                return val
            },
            set(newVal) {
                if (newVal !== val) {
                    val = newVal
                    _this.walk(newVal)

                }
            }
        })

    }

}



